package com.leyou.common.advice;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.vo.ExceptionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author xiuer
 * @version V1.0.0
 * @package com.leyou.common.advice
 * @description 统一异常拦截器
 * @date 2019/6/22 19:10
 * 该注解会拦截所有加了@Controller的类 @ControllerAdvice
 */

@ControllerAdvice
@Slf4j
public class BasicExceptionAdvice {
    /**
     * 该注解@ExceptionHandler作用：被声名的方法是一个处理器
     */
    @ExceptionHandler(LyException.class)
    public ResponseEntity<ExceptionResult> handleException(LyException e) {
        return ResponseEntity.status(e.getStatus()).body(new ExceptionResult(e));
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleRuntimeException(RuntimeException e) {
        return ResponseEntity.status(500).body(e.getMessage());
    }
}
